package ac.github.os.internal.core.store

import taboolib.common.platform.function.getDataFolder
import taboolib.module.configuration.SecuredFile
import java.io.File

class StorageLocal : Storage{

    lateinit var file : File
    lateinit var config : SecuredFile

    override fun load() {
        file = File(getDataFolder(),"data.yml")
        if (!file.exists()) {
            file.createNewFile()
        }
        config  = SecuredFile.Companion.loadConfiguration(file)
    }

    override fun set(key: String, data: Store.Data) {
        config.set(key,data.asString())
        config.saveToFile()
    }

    override fun findStore(): Store {
        return Store().apply {
            config.getStringList("keys").forEach {
                set(it, Store.Data(config.getString(it)))
            }
        }
    }

    override fun remove(key: String) {
        config.set(key,null)
    }
}